<?php

namespace app\api\controller\lottery;

use app\admin\model\Lottery;
use app\admin\model\LotteryAcc;
use app\api\controller\Base;

/**
 * 抽奖小程序相关接口
 */
class Lotterys extends Base
{

    // 新增抽奖数据
    public function set_lottery()
    {
        $owner = $this->request->post('owner');
        $title = $this->request->post('title');
        $cover = $this->request->post('cover');
        $type = $this->request->post('type');
        $condition = $this->request->post('condition');
        $detail = $this->request->post('detail');

        $l = new Lottery();
        $l->owner = $owner;
        $l->title = $title;
        $l->cover = $cover;
        $l->type = $type;
        $l->condition = $condition;
        $l->detail = $detail;
        $l->user_list = '[]';
        $l->user_count = 0;
        $res = $l->save();
        $this->result($res, $l->id);
    }

    // 查询抽奖数据
    public function get_lottery()
    {
        $lid = $this->request->post('lid');
        $uid = $this->request->post('uid');
        // 查询抽奖
        $lottery = Lottery::get($lid);
        // 判断是否有中奖者,有则跳过
        if (!$lottery->lucker) {
            // 判断开奖条件,进而开奖
            $user_list = json_decode($lottery->user_list);
            $user_count = $lottery->user_count;
            if ($lottery->type) {
                if ($user_count >= $lottery->condition) {
                    $lucker = rand(0, $user_count - 1);
                    $lottery->lucker = $user_list[$lucker];
                    $a = LotteryAcc::get($user_list[$lucker]);
                    $a->lucky += 1;
                    $a->save();
                }
            } else {
                if (time() >= $lottery->condition - 1) {
                    $lucker = rand(0, $user_count);
                    $lottery->lucker = $user_list[$lucker];
                    $a = LotteryAcc::get($user_list[$lucker]);
                    $a->lucky += 1;
                    $a->save();
                }
            }
            $lottery->save();
        }

        // 判断当前用户是否参加过
        $acc = LotteryAcc::get($uid);
        $all_lottery = json_decode($acc->all_lottery);
        $msg = false;
        foreach ($all_lottery as $item) {
            if ($item == $lid) {
                $msg = true;
            }
        }

        $this->result($msg, $lottery);
    }

    // 参加抽奖
    public function join_lottery()
    {
        $lid = $this->request->post('lid');
        $uid = $this->request->post('uid');

        // 更新抽奖表
        $lottery = Lottery::get($lid);
        $user_list = json_decode($lottery->user_list);
        array_push($user_list, $uid);
        $lottery->user_list = json_encode($user_list);
        $lottery->user_count += 1;

        $res = 0;
        if ($lottery->save()) {
            // 更新用户表
            $acc = LotteryAcc::get($uid);
            $all_lottery = json_decode($acc->all_lottery);
            array_push($all_lottery, $lid);
            $acc->all_lottery = json_encode($all_lottery);
            $res = $acc->save();
        };

        $this->success($res);
    }

    // 查看所有参加者头像
    public function get_avatar_list()
    {
        $lid = $this->request->post('lid');
        $page = $this->request->post('page');
        $lottery = Lottery::get($lid);
        $user_list = json_decode($lottery->user_list);

        $list = array_slice($user_list, $page * 40, 40);

        $data = array();
        foreach ($list as $item) {
            $acc = LotteryAcc::get($item);
            array_push($data, $acc->avatar_url);
        }

        $this->result('ok', $data);
    }

    // 获取当前用户参加的抽奖信息
    public function get_lottery_list()
    {
        $uid = $this->request->post('uid');
        $acc = LotteryAcc::get($uid);
        $list = json_decode($acc->all_lottery);

        $data = array();
        foreach ($list as $item) {
            $l = Lottery::get($item);
            array_push($data, $l);
        }

        $this->result('list', array_reverse($data));
    }

}
